/*
 * @Author: 肖芬芳 1911683843@qq.com
 * @Date: 2022-11-17 10:05:30
 * @LastEditors: 肖芬芳 1911683843@qq.com
 * @LastEditTime: 2022-12-09 16:55:16
 * @FilePath: \blog\src\router\index.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import { createRouter, createWebHashHistory, RouteRecordRaw } from 'vue-router'
import HomeView from '@/views/admin/home/index.vue'
import sys from './modules/system'
import goods from "./modules/goods"
import nprogress from 'nprogress'
import 'nprogress/nprogress.css'

const routes: Array<RouteRecordRaw> = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    name: 'admin/login',
    component: () => import("@/views/admin/login/index.vue")
  },
  {
    // 首页路由
    path: '/admin/home',
    name: 'home',
    component: HomeView
  },
  {
    // 系统路由
    path: '/admin/sys',
    name: 'sys',
    component: HomeView,
    meta: {
      title: '系统管理'
    },
    children: sys.children

  },
  {
    // 商品路由
    path: '/admin/goods',
    name: 'goods',
    component: HomeView,
    meta: {
      title: '商品管理'
    },
    children: goods.children

  },
  //网页-登录
  {
    path: '/home/login',
    name: 'home/login',
    component: () => import('@/views/home/login/index.vue')
  },
]

const router = createRouter({
  // hash模式的路由
  history: createWebHashHistory(),
  routes
})

// 路由拦截，形成进度条
router.beforeEach((to, from, next) => {
  nprogress.start()
  // 路由导航守卫
  // to:将要访问的路径
  // from：代表从哪个路径跳转
  // next：函数，next()表示放行，next('/login')强制跳转
  if (to.path === '/login') return next()
  // 获取token
  const tokenStr = window.localStorage.getItem('t')
  if (!tokenStr) return next('/login')
  next()
})
router.afterEach(() => {
  nprogress.done()
})

export default router
